﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CTIClassLibrary;

namespace CTIPayService
{
    public partial class CiticList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string str = DateTime.Today.AddDays(-1).ToShortDateString();
                string str2 = DateTime.Today.ToShortDateString();

                this.txtDateStart.Text = str;
                this.txtDateEnd.Text = str2;

                str2 += " 23:59:59";
                BindGrid(str, str2);
            }
        }

        private void BindGrid(string dateStart,string dateEnd)
        {
            this.sdsPay.SelectParameters["date1"].DefaultValue = dateStart;
            this.sdsPay.SelectParameters["date2"].DefaultValue = dateEnd;
            this.RadGridPay.DataBind();

            this.sdsLog.SelectParameters["date1"].DefaultValue = dateStart;
            this.sdsLog.SelectParameters["date2"].DefaultValue = dateEnd;
            this.RadGridLog.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            this.RadGridPay.AllowPaging = false;
            this.RadGridPay.MasterTableView.ExportToExcel();
        }

        protected void btnQueryPay_Click(object sender, EventArgs e)
        {
            string str = this.txtDateStart.Text.Trim();
            string str2 = this.txtDateEnd.Text.Trim() + " 23:59:59";
            BindGrid(str, str2);
        }

        protected void RadGridPay_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is Telerik.Web.UI.GridFooterItem)
            {
                string str = this.txtDateStart.Text.Trim();
                string str2 = this.txtDateEnd.Text.Trim();

                if (string.IsNullOrEmpty(str))
                {
                    str = DateTime.Today.ToShortDateString();
                }
                if (string.IsNullOrEmpty(str2))
                {
                    str2 = DateTime.Today.ToShortDateString() + " 23:59:59";
                }
                else
                    str2 += " 23:59:59";

                string str3 = Common.DB.Select(Tables.t_CITIC_Transfer, Tables.t_CITIC_Transfer.TransAmt.Sum())
                    .Where(Tables.t_CITIC_Transfer.TransType == "支付")
                    .Where(Tables.t_CITIC_Transfer.TransDatetime.Between(str, str2))
                    .ToScalar().ToString();
                if (string.IsNullOrEmpty(str3))
                {
                    str3 = "0";
                }

                string str4 = Common.DB.Select(Tables.t_CITIC_Transfer, Tables.t_CITIC_Transfer.TransAmt.Sum())
                    .Where(Tables.t_CITIC_Transfer.TransType == "退款")
                    .Where(Tables.t_CITIC_Transfer.TransDatetime.Between(str, str2))
                    .ToScalar().ToString();
                if (string.IsNullOrEmpty(str4))
                {
                    str4 = "0";
                }
                decimal num = Convert.ToDecimal(str3);
                decimal num2 = Convert.ToDecimal(str4);
                string format = "<pan style='color:red'>共支付 {0}，其中退款 {1}，实收 {2}</pan>";
                format = string.Format(format, num, num2, num - num2);
                Telerik.Web.UI.GridFooterItem item = e.Item as Telerik.Web.UI.GridFooterItem;

                for (int i = 3; i < 15; i++)
                {
                    item.Cells.RemoveAt(3);
                }
                item.Cells[2].Text = format;
                item.Cells[2].ColumnSpan = 12;
                item.Cells[2].ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}
